Libraries:

SPDS:

library(tidyverse)
library(sf)
library(units)

Data

library(USAboundaries)
library(rnaturalearth)

Visualization

library(gghighlight)
library(ggrepel)
library(knitr)

Question 1

1.1 - Projection
eqdc = '+proj=eqdc +lat_0=40 +lon_0=-96 +lat_1=20 +lat_2=60 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs'
1.2 - USA State Boundaries
remotes::install_github("ropensci/USAboundaries")
remotes::install_github("ropensci/USAboundariesData")

region = data.frame(region = state.region,
                    state_name = state.name)

conus = USAboundaries::us_states(resolution = "low") %>%
  left_join(region) %>%
  filter(!state_name %in% c("Puerto Rico", "Alaska", "Hawaii")) %>% 
  st_transform(eqdc)
plot(conus['aland'])

1.3 - Country Boundaries
remotes::install_github("ropenscilabs/rnaturalearthdata")

countries = rnaturalearth::countries110 %>% 
  st_as_sf(coords = c("lng", "lat"), crs = 4326) %>% 
  filter(admin %in% c("Mexico", "United States of America", "Canada")) %>% 
  st_transform(eqdc)
1.4 - City Locations
cities = readr::read_csv("../data/uscities.csv") %>%
  st_as_sf(coords = c("lng", "lat"), crs = 4326) %>%
  filter(!state_name %in% c("Hawaii", "Puerto Rico", "Alaska")) %>%
  st_transform(eqdc)
plot(cities$geometry, pch = 16, cex = 0.2)


Question 2:

2.1 - Distance to USA Border (km)
conus_u = st_union(conus) %>%
  st_cast("MULTILINESTRING") %>%
  st_transform(5070)

cities = st_transform(cities, 5070)

cities = cities %>% 
  mutate(dist_border = st_distance(cities, conus_u),
         dist_border = units::set_units(dist_border, "km"),
         dist_border = units::drop_units(dist_border))
  
farthest_us_border = cities %>%   
  slice_max(dist_border, n= 5) %>% 
  select(city, state_name, dist_border)

knitr::kable(farthest_us_border,
             caption = "Cities Farthest from USA border",
             col.names = c("City", "State", "Distance to USA Border (km)", "Coordinates")) %>% 
  kableExtra::kable_styling("striped", full_width = TRUE)
Cities Farthest from USA border
City State Distance to USA Border (km) Coordinates
Hill City Kansas 1035.632 POINT (-328255.9 1822709)
Palco Kansas 1032.352 POINT (-304610.9 1809000)
Dresden Kansas 1031.450 POINT (-375765.6 1853336)
Jennings Kansas 1030.252 POINT (-364803.1 1859429)
Bogue Kansas 1028.555 POINT (-314769.5 1821299)
2.2 - Distance to States (km)
conus_c = st_combine(conus) %>%
  st_cast("MULTILINESTRING") %>%
  st_transform(5070)

cities = cities %>% 
  mutate(dist_state = st_distance(cities, conus_c),
         dist_state = units::set_units(dist_state, "km"),
         dist_state = units::drop_units(dist_state))

farthest_state = cities %>%   
  slice_max(dist_state, n= 5) %>% 
  select(city, state_name, dist_state)

knitr::kable(farthest_state,
             caption = "Cities Farthest from State Borders",
             col.names = c("City", "State", "Distance to State Border (km)", "Coordinates")) %>% 
  kableExtra::kable_styling("striped", full_width = TRUE)
Cities Farthest from State Borders
City State Distance to State Border (km) Coordinates
Lampasas Texas 311.6903 POINT (-207612.9 889968.2)
Bertram Texas 306.8676 POINT (-196658.9 854001.9)
Kempner Texas 304.9356 POINT (-187640.5 890810.8)
Florence Texas 301.8986 POINT (-171013.9 864355.8)
Harker Heights Texas 300.6722 POINT (-156461.2 888181.7)
2.3 - Distance to Mexico (km)
mexico = countries %>% 
  filter(admin == "Mexico") %>% 
  st_transform(5070)

cities = cities %>% 
  mutate(dist_mexico = st_distance(cities, mexico),
         dist_mexico = units::set_units(dist_mexico, "km"),
         dist_mexico = units::drop_units(dist_mexico))

farthest_mexico = cities %>%
  slice_max(dist_mexico, n= 5) %>%
  select(city, state_name, dist_mexico)

knitr::kable(farthest_mexico,
             caption = "Cities Farthest from Mexican Border",
             col.names = c("City", "State", "Distance to Mexican Border (km)", "Coordinates")) %>%
  kableExtra::kable_styling("striped", full_width = TRUE)
Cities Farthest from Mexican Border
City State Distance to Mexican Border (km) Coordinates
Caribou Maine 3292.785 POINT (2113484 2966597)
Presque Isle Maine 3277.282 POINT (2119585 2947688)
Calais Maine 3181.785 POINT (2227000 2800397)
Eastport Maine 3174.086 POINT (2249785 2781852)
Old Town Maine 3090.254 POINT (2119967 2746384)
2.4 - Distance to Canada (km)
canada = countries %>% 
  filter(admin == "Canada") %>% 
  st_transform(5070)

cities = cities %>% 
  mutate(dist_canada = st_distance(cities, canada),
         dist_canada = units::set_units(dist_canada, "km"),
         dist_canada = units::drop_units(dist_canada))

farthest_canada = cities %>%
  slice_max(dist_canada, n= 5) %>%
  select(city, state_name, dist_canada)

knitr::kable(farthest_canada,
             caption = "Cities Farthest from Canadian Border",
             col.names = c("City", "State", "Distance to Canadian Border (km)", "Coordinates")) %>%
  kableExtra::kable_styling("striped", full_width = TRUE)
Cities Farthest from Canadian Border
City State Distance to Canadian Border (km) Coordinates
Guadalupe Guerra Texas 2255.590 POINT (-309846.5 377887.3)
Sandoval Texas 2254.783 POINT (-309568.9 378692.2)
Fronton Texas 2253.929 POINT (-309210.9 379494.5)
Fronton Ranchettes Texas 2251.062 POINT (-304258 379302.4)
Evergreen Texas 2250.952 POINT (-303978.7 379227.5)

Question 3

3.1
cities_top10 = cities %>% 
  slice_max(population, n = 10)

ggplot() +
  geom_sf(data = countries, fill = "gray80") +
  geom_sf(data = conus_u, color = "red", cex = 1) +
  geom_sf(data = conus_c, color = "black") +
  geom_sf(data = cities_top10, aes(size = population), color = "red") +
  theme_linedraw() +
  theme(plot.background = element_rect(fill = "white"),
        panel.background = element_rect(fill = "slategray1")) +
  labs(title = "Data")

3.2
ggplot() +
  geom_sf(data = conus) +
  geom_sf(data = cities, aes(col = dist_border), size = 0.1) +
  geom_sf(data = farthest_us_border, col = "red", size = 1) +
  geom_sf(data = conus_u, col = "black") +
  scale_color_gradient(low = "grey", high = "darkblue") +
  labs(title = "Distance of US Cities from US Border",
       dist_border = "Distance to US Border") +
  ggthemes::theme_map() +
  ggrepel::geom_label_repel(data = farthest_us_border,
                            aes(label = city, geometry = geometry),
                            stat = "sf_coordinates",
                            size = 4)

3.3
ggplot() +
  geom_sf(data = conus) +
  geom_sf(data = cities, aes(col = dist_state), size = 0.2) +
  geom_sf(data = farthest_state, col = "navyblue", size = 1) +
  scale_color_gradient(low = "lightcoral", high = "red4") +
  labs(title = "Distance of US Cities from US Border",
       dist_border = "Distance to US Border") +
  ggthemes::theme_map() +
  ggrepel::geom_label_repel(data = farthest_state,
                            aes(label = city, geometry = geometry),
                            stat = "sf_coordinates",
                            size = 4)

3.4
eqdistance = cities %>% 
  mutate(ab_distance = abs(dist_mexico - dist_canada)) %>% 
  filter(ab_distance <= 100)

eqdistance_5 = eqdistance %>% 
  slice_max(population, n = 5)

ggplot() +
  geom_sf(data = conus) +
  geom_sf(data = eqdistance, aes(col = ab_distance), size = 0.2) +
  geom_sf(data = eqdistance_5, col = "red", size = 2) +
  labs(title = "US Cities Equidistant From Mexican and Canadian Borders") +
  ggthemes::theme_map() +
  ggrepel::geom_label_repel(data = eqdistance_5,
                            aes(label = city, geometry = geometry),
                            stat = "sf_coordinates",
                            size = 3)


Question 4

zone = cities %>% 
  filter(dist_border <= 160) %>% 
  mutate(sum_pop = sum(population))

zone_10 = zone %>% 
  group_by(state_name) %>% 
  slice_max(population, n = 1)

cities %>% 
  summarise(tot_pop = sum(population))
## Simple feature collection with 1 feature and 1 field
## geometry type:  MULTIPOINT
## dimension:      XY
## bbox:           xmin: -2345647 ymin: 279498.5 xmax: 2249785 ymax: 3170440
## projected CRS:  NAD83 / Conus Albers
## # A tibble: 1 x 2
##     tot_pop                                                             geometry
##       <dbl>                                                     <MULTIPOINT [m]>
## 1 397213686 ((-2345647 2109315), (-2343289 2115862), (-2341184 2149870), (-2340…
259 / 397 * 100
## [1] 65.23929
ggplot() +
  geom_sf(data = conus) +
  geom_sf(data = zone) +
  scale_color_gradient(low = "orange", high = "darkred") +
  geom_sf(data = zone_10, col = "navyblue", size = 1) +
  labs(title = "Border Zone",
       subtitle = "100 Mile Zone / Danger Zone") +
  ggthemes::theme_map() +
  ggrepel::geom_label_repel(data = zone_10,
                            aes(label = city, geometry = geometry),
                            stat = "sf_coordinates",
                            size = 3)

Constitutional rights protected by the Fourth Constitutional Amendment no longer apply fully at borders. The border zone of 100 miles (~160 km) from external boundaries contains 28,145 cities. 259 million people live in these border zones. Compared to the 397 million people living in the US, 65 % of the US population, or about two thirds, is living in these border zones. This is accurate to the ACLU article estimate.